<template>
  <div>
    <el-dialog :title="title" :visible.sync="$parent.openArea" width="60%" append-to-body>
      <div class="center_dialog_children">
        <el-form :model="queryParams" ref="queryForm" size="medium" :inline="true" label-width="60px">
          <el-form-item label="" prop="areaNum">
            <el-input v-model="queryParams.areaNum" placeholder="请输入区域编号" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="" prop="areaName">
            <el-input v-model="queryParams.areaName" placeholder="请输入区域名称" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">
              搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table ref="multipleTable" v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column label="序号" align="center" prop="order" width="50" />
          <el-table-column label="区域编号" align="center" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.areaNum || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="区域名称" align="center" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.areaName || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="包含位置" align="center" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-for="(item, index) in scope.row.places" :key="index">
                {{ item.placeName || '-' }}({{ item.placeNum || '-' }})、
              </span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="最后打印时间" align="center" min-width="150">
            <template slot-scope="scope">
              {{ scope.row.lastPrintTime || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" min-width="130" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.remark || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="创建部门" align="center" min-width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.deptName || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" min-width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.createBy || '-' }}
            </template>
          </el-table-column> -->
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
        <el-divider></el-divider>
        <el-form ref="form" :model="form" :rules="rules" label-width="90px" :inline="true">
          <el-form-item label="巡检员" prop="inspectorId">
            <el-select v-model="form.inspectorId" multiple placeholder="请选择巡检员" style="width:100%">
              <el-option v-for="(dict, index) in userList" :key="index" :label="dict.userName" :value="dict.userId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="巡检时间" prop="inspectionTime">
            <el-time-picker is-range value-format="HH:mm" v-model="form.inspectionTime" range-separator="至"
              start-placeholder="开始时间" end-placeholder="结束时间" placeholder="选择时间范围">
            </el-time-picker>
          </el-form-item>
        </el-form>
      </div>
      <div align="center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="() => $parent.openArea = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
// import { getSelectplanAreaList } from "@/api/equipmentInspct/index.js";
import { arealist, getuserlist } from "@/api/equipment/equipment.js";
// import { getuserlist } from '@/api/equipment/equipment.js'


export default {
  name: "ChildrenArea",
  props: ['title', 'AreaObj', 'index'],
  components: {
  },
  data () {
    return {
      queryParams: {
        areaNum: null,
        areaName: null,
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'areaId',//排序字段
        isAsc: 'desc'//倒叙
      },
      form: {
        inspectorId: '',
        inspectionTime: ''
      },
      selection: [],
      userList: [],//巡检人列表
      loading: false,
      noticeList: [],
      // 总条数
      total: 0,
      rules: {
        inspectorId: { required: true, message: '必填', trigger: 'blur' },
        inspectionTime: { required: true, message: '必填', trigger: 'blur' },
      }
    };
  },
  created () {
    this.getList()
    this.userinit()
  },
  methods: {
    async getList () {
      this.loading = true;
      // let obj = value || {}
      let params = {
        ...this.queryParams,
        // ...obj,
        // 'equipmentBaseVo.equipmentName': obj.equipmentName
      }
      //  let data = await getdatalist({ dictType: 'base_unit' })
      //  this.dictdatalist=data.data
      arealist(params).then(response => {
        this.noticeList = response.data && response.data.map((item, index) => {
          return {
            ...item,
            order: index + 1
          }
        });
        this.total = response.total;
        this.loading = false;
        this.updateValue()
      });
    },
    updateValue () {
      if (this.title === '修改巡检区域') {
        this.selection = this.AreaObj.selection
        this.form.inspectorId = this.AreaObj.inspectorId
        this.form.inspectionTime = this.AreaObj.inspectionTime.split('~')
        this.noticeList.forEach(item => {
          this.AreaObj.selection.forEach(i => {
            if (i.areaId === item.areaId) {
              this.$nextTick(() => {
                this.$refs.multipleTable.toggleRowSelection(item, true);
              })
            }
          })

        });
      }
    },
    async userinit () {
      let userdata = await getuserlist({ status: 0 });//负责人列表
      this.userList = userdata.data
    },
    handleSelectionChange (selection) {
      this.selection = selection
    },
    /**提交 */
    submitForm () {
      if (this.selection.length === 0) {
        return this.$message.error('请选择巡检区域')
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let Obj = { ...this.form, selection: this.selection, inspectionTime: this.form.inspectionTime.join('~') }
          this.$parent.openArea = false
          let dept = []
          let user = []
          this.userList.map(item => {
            this.form.inspectorId.map(i => {
              if (i === item.userId) {
                user.push(item.userName)
                dept.push(item.dept.deptName)
              }
            })
          })
          let Arr = { ...Obj, userArr: user, deptArr: dept, index: this.title === '修改巡检区域' ? this.index : undefined }
          this.$emit('Commit', Arr)
        }
      })
      // console.log(this.form);
    },
    resetQuery () {
      this.queryParams.areaNum = null
      this.queryParams.areaName = null
      this.getList()
    },
    handleQuery () {
      this.getList()
    }
  }
};
</script>

<style lang="scss" scoped>
.center_dialog_children {
  // text-align: center;
  width: 95%;
  margin: 0px auto;
}
</style>
